What is create-error?
The create-error npm package is a utility for creating custom error types in JavaScript. It simplifies the process of defining and using custom error classes, making it easier to handle specific error conditions in your applications.
What are create-error's main functionalities?
Creating a Basic Custom Error
This feature allows you to create a basic custom error type with a specific name. The custom error can then be thrown and caught like any standard error.
const createError = require('create-error');
const MyCustomError = createError('MyCustomError');
try {
throw new MyCustomError('Something went wrong!');
} catch (err) {
console.error(err.name); // MyCustomError
console.error(err.message); // Something went wrong!
}
Creating a Custom Error with Additional Properties
This feature allows you to create a custom error type with additional properties. In this example, a `statusCode` property is added to the custom error.
const createError = require('create-error');
const DetailedError = createError('DetailedError', { statusCode: 400 });
try {
throw new DetailedError('Invalid input!');
} catch (err) {
console.error(err.name); // DetailedError
console.error(err.message); // Invalid input!
console.error(err.statusCode); // 400
}
Inheriting from Custom Errors
This feature allows you to create custom error types that inherit from other custom error types. This is useful for creating a hierarchy of error types in your application.
const createError = require('create-error');
const AppError = createError('AppError');
const NotFoundError = createError('NotFoundError', AppError);
try {
throw new NotFoundError('Resource not found!');
} catch (err) {
console.error(err.name); // NotFoundError
console.error(err.message); // Resource not found!
console.error(err instanceof AppError); // true
}
Other packages similar to create-error
http-errors
The http-errors package is used to create HTTP errors for use in Express and other web frameworks. It provides a set of predefined HTTP error classes and allows for the creation of custom HTTP errors. Compared to create-error, http-errors is more focused on HTTP-specific error handling.
custom-error-generator
The custom-error-generator package provides a simple way to create custom error classes with additional properties. It is similar to create-error in terms of functionality but offers a different API for defining custom errors.
extendable-error-class
The extendable-error-class package allows you to create custom error classes that can be extended. It provides a base class for creating custom errors and is similar to create-error in terms of creating hierarchical error types.
create-error.js
A simple helper for creating subclassed errors in Javascript.
Use:
$ npm install create-error
$ bower install create-error
var createError = require('create-error');
var MyCustomError = createError('MyCustomError');
var SubCustomError = createError(MyCustomError, 'CoolSubError', {messages: []});
var sub = new SubCustomError('My Message', {someVal: 'value'});
sub instanceof SubCustomError
sub instanceof MyCustomError
sub instanceof Error
assert.deepEqual(sub.messages, [])
assert.equal(sub.someVal, 'value')
createError(name, [properties])
Creates a new error by specifying the name of the error to be created,
taking an optional hash of properties to be attached to the error class
upon creation.
createError(Target, [name, [properties]])
Create a new error by specifying the Target
error class we wish to inherit from,
along with an optional name and properties for the error. If the name
is omitted,
it will have the same name as the parent error.
Additional Notes:
In the browser, the function will be assigned to window.createError
,
and createError.noConflict()
will restore the original window.createError
if overwritten.
License
MIT